﻿using System;
using System.Collections;

namespace MvcCms.Data.Pagination
{
    public class Pagination : IPagination
    {
        #region Member Variables
        protected int _pageNumber;
        protected int _totalItems;
        protected int _totalDisplayItems;
        protected int _pageSize;
        #endregion
        //methods
        public IEnumerator GetEnumerator()
        {
            throw new NotImplementedException("The method or operation is not implemented.");
        }
        #region Public Properties

        public bool HasNextPage
        {
            get { return PageNumber < TotalPages; }
        }
        public bool HasPreviousPage
        {
            get { return PageNumber > 1; }
        }
        public int LastItem
        {
            get 
            {
                if (TotalDisplayItems > 0)
                {
                    var numberToReturn = FirstItem + PageSize - 1;
                    if (numberToReturn > TotalDisplayItems) { return TotalDisplayItems; }
                    else { return numberToReturn; }
                }
                else { return 0; }
            }
        }
        public int FirstItem
        {
            get
            {
                if (TotalDisplayItems > 0)
                {
                    return ((PageNumber - 1) * PageSize) + 1;
                }
                else { return 0; }
            }
        }
        public int PageSize
        {
            get { return _pageSize; }
            set { _pageSize = value; }
        }
        public int TotalItems
        {
            get { return _totalItems; }
            set { _totalItems = value; }
        }
        public int TotalDisplayItems
        {
            get { return _totalDisplayItems; }
            set { _totalDisplayItems = value; }
        }
        public int TotalPages
        {
            get { return (int)Math.Ceiling(((double)TotalDisplayItems) / PageSize); }
        }
        public int PageNumber
        {
            get 
            {
                if (TotalDisplayItems > 0)
                {
                    return _pageNumber;
                }
                else { return 0; }
            }
            set { _pageNumber = value; }
        }
        #endregion

    }

    public class DataTablesPagination
    {
        protected int _iTotalRecords;
        protected int _iTotalDisplayRecords;

        public IEnumerator GetEnumerator()
        {
            throw new NotImplementedException("The method or operation is not implemented.");
        }
        #region Public Properties

        /// <summary>Total records, before filtering (i.e. the total number of records in the database)</summary>
        public int iTotalRecords
        {
            get { return _iTotalRecords; }
            set { _iTotalRecords = value; }
        }
        /// <summary>Total records, after filtering (i.e. the total number of records after filtering has been applied - not just the number of records being returned in this result set)</summary>
        public int iTotalDisplayRecords
        {
            get { return _iTotalDisplayRecords; }
            set { _iTotalDisplayRecords = value; }
        }   
        #endregion
    }
}